package com.ashin.Langchat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ashin.entity.bo.LinkAiVo;
import com.ashin.util.HttpUtils;
import com.theokanning.openai.completion.chat.ChatMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class LangChatProxy {
    public static void main(String[] args){
        LangChatProxy aiClient =  new LangChatProxy();
        List<ChatMessage> prompt =  new ArrayList<>();
        prompt.add(new ChatMessage("user","晚上好呀！"));
        aiClient.send(prompt);
    }
    private String api="https://api.link-ai.chat/v1/chat/completions";
    private String apiKey = "Bearer Link_aZiw18EChQDwqOnY5j22bKQYSWnV5XmqkqXnGkd1dS";


    public String send(List<ChatMessage> prompt) {
        CloseableHttpClient httpClient = HttpUtils.createSSLClientDefault();
        CloseableHttpResponse httpResponse = null;
        HttpPost httpPost = null;
        try {
            httpPost = HttpUtils.getHttpPost(api , null);
            httpPost.setHeader("Authorization", apiKey);
            httpPost.setEntity(new StringEntity(getParam(prompt), "UTF-8"));
            httpResponse = httpClient.execute(httpPost);
//{"choices":[{"index":0,"message":{"role":"assistant","content":"晚上好！有什么问题我可以帮你解答吗？"},"finish_reason":"stop"}]}
            String resCcontent = responseContent(EntityUtils.toString(httpResponse.getEntity()));
            log.debug(resCcontent);
            return resCcontent;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            HttpUtils.closeHttp(httpClient, httpResponse);
        }
        return "";
    }
    private String responseContent(String httpResponseJson){
        // 解析JSON字符串
        JSONObject jsonObject = JSON.parseObject(httpResponseJson);
        JSONArray choicesArray = jsonObject.getJSONArray("choices");
        if (choicesArray != null && choicesArray.size() > 0) {
            JSONObject choiceObject = choicesArray.getJSONObject(0);
            JSONObject messageObject = choiceObject.getJSONObject("message");
            String content = messageObject.getString("content");
            return content;
        }
        return "";
    }
    public String getParam(List<ChatMessage> prompts) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("app_code", "b967lwxx");
        List<LinkAiVo> linkAiVoList = new ArrayList<>();
        for (ChatMessage chat:prompts) {
            linkAiVoList.add(new LinkAiVo(chat.getRole(), chat.getContent()));
        }
        objectMap.put("messages", linkAiVoList);
        objectMap.put("model", "gpt-3.5-turbo");
//        objectMap.put("top_p", linkAiVoList);
//        objectMap.put("temperature", linkAiVoList);
//        objectMap.put("frequency_penalty", linkAiVoList);
//        objectMap.put("presence_penalty", linkAiVoList);
//        objectMap.put("stream", linkAiVoList);

        return JSON.toJSONString(objectMap);
    }
}
